#!/bin/bash
Green="\033[32m"
Greens="\033[34m"
Red="\033[31m"
GreenBG="\033[42;37m"
RedBG="\033[41;37m"
Font="\033[0m"
cur_path="$(pwd)"
execs() {
    local name=$(docker ps | grep "/$1" | grep "wfs" | awk '{print $1}')
    docker exec -it "$name" /bin/sh -c "$2"
}
env() {
    local value=`cat ${cur_path}/.env | grep "^$1 = " | awk -F ' = ' '{print $2}'`
    echo "$value"
}

####################################################################################
####################################################################################
####################################################################################

if [ $# -gt 0 ]; then
    # 初始化
    if [[ "$1" == "init" ]] || [[ "$1" == "install" ]]; then
        sh ./docker/init.sh

    # 重装容器
    elif [[ "$1" == "up" ]]; then
        docker compose up -d

    # 重启
    elif [[ "$1" == "restart" ]]; then
        if [[ "$2" ]]; then
            docker compose restart "$2" 
        else
            docker compose restart
        fi

    # 启动网页
    elif [[ "$1" == "dev" ]]; then
        docker compose restart
        # echo "----------------------------------------------------------------------"
        echo -e "${Green} 启动成功 ${Font}"
        echo -e "${Green} 地址： http://127.0.0.1:$(env APP_PORT) ${Font}"
        echo -e "${Green} 账号： admin ${Font}"
        echo -e "${Green} 密码： 1 ${Font}"

    # 进入容器 ./cmd d | ./cmd d php
    elif [[ "$1" == "d" ]]; then
        if [[ "$2" ]]; then
            docker exec -it $(docker ps | grep "/$2" | grep "wfs" | awk '{print $1}') /bin/bash
        else
            docker ps | grep "wfs" | awk '{print NR,$1,$2}'
            read -p "请输入容器编号：" uname
            num=$[$uname+1]
            docker exec -it $(docker ps | sed -n "$num p"| grep "wfs" | awk '{print $1}') /bin/bash
        fi
    
    # 调试
    elif [[ "$1" == "test" ]]; then
        execs php "php think test";

    # 数据库操作
    elif [[ "$1" == "db" ]]; then
        if [ "$2" ]; then
            uname=$2
        else
            echo -e "${Greens}1${Font}    重构数据库"
            echo -e "${Greens}2${Font}    重构数据库并生成初始化数据"
            read -p "请输入操作编号：" uname
        fi   
        if [ "$uname" == "1" ]; then
            execs php "php think migrate:refresh";
        elif [ "$uname" == "2" ]; then
           execs php "php think migrate:refresh --seed";
        fi   

    # 启动cron
    elif [[ "$2" == "cron" ]]; then
        execs php "cron start"

    # 原生php命令
    elif [[ "$1" == "php" ]]; then
        execs $1 "$*"

    # 查看容器
    elif [[ "$1" == "ps" ]]; then
        docker ps | grep "wfs"
    fi

else
    echo -e "${Greens} dev ${Font}             重启动"
    echo -e "${Greens} restart ${Font}         重启容器"
    echo -e "${Greens} d ${Font}               进入容器"
    echo -e "${Greens} test ${Font}            调试"
    echo -e "${Greens} db ${Font}              数据库操作"
    echo -e "${Greens} php \$1 ${Font}         执行php命令"
    echo -e "${Greens} ps ${Font}              查看容器列表"
    echo -e "${Greens} init ${Font}            初始化/安装系统"
    echo -e "${Greens} up ${Font}              重装容器"
    read -p "请输入动作编号：" uname
    ./cmd $uname
fi
